package com.xyhy.core.base.lookup;

import cn.hutool.core.comparator.PinyinComparator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Transient;
import lombok.Getter;
import lombok.Setter;

import java.util.Comparator;

@Getter
@Setter
public abstract class LookupKeyVal<ID> implements Comparable<LookupKeyVal<ID>> {
  @Transient
  @JsonIgnore
  public PinyinComparator pinyinComparator = new PinyinComparator();

  protected ID id;
  private String name;
  private Integer orderNo;

  public LookupKeyVal() {
  }

  public LookupKeyVal(ID id, String name) {
    this.id = id;
    this.name = name;
  }

  public abstract ID getId();

  @Override
  public int compareTo(LookupKeyVal<ID> o) {
    final Comparator<? super LookupKeyVal<ID>> comparator = Comparator
            .comparing(LookupKeyVal<ID>::getName, Comparator.nullsFirst(pinyinComparator))
            .thenComparing(LookupKeyVal::getOrderNo, Comparator.nullsLast(Comparator.comparingInt(Integer::intValue)));

    return comparator.compare(this, o);
  }

}
